package com.healthcloud.utils;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

public class SmartUtil {

//	public static String[] getTestDataString(String data){
//		String[] arr = null;
//		if(!StringUtils.ifNull(data)){
//			arr = data.split("&");
//		}
//		return arr;
//	}
	public static final String byte2hex(byte b[]) {
        if (b == null) {
            throw new IllegalArgumentException(
                    "Argument b ( byte array ) is null! ");
        }
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = Integer.toHexString(b[n] & 0xff);
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
        return hs.toUpperCase();
    }
	public static String getStreamString(HttpServletRequest request) throws IOException{
		InputStream in = request.getInputStream(); 
		int streamLength = request.getContentLength(); 
		byte[] b = new byte[streamLength];  
		int temp=0; 
		int len = 0;
        while((temp=in.read())!=-1){//当没有读取完时，继续读取   
            b[len]=(byte)temp;   
            len++;   
        }   
        in.close();  
        return new String(b,0,len);
	}
	
	public static String getRequestPayload(HttpServletRequest req) {
        StringBuilder sb = new StringBuilder();  
        try(BufferedReader reader = req.getReader();) {  
                 char[] buff = new char[1024];  
                 int len;  
                 while((len = reader.read(buff)) != -1) {  
                          sb.append(buff,0, len);  
                 }  
        }catch (IOException e) {  
                 e.printStackTrace();  
        }  
        return sb.toString();  
	} 
	
//	/**
//	 * 插入成功后返回信息
//	 * @param status 状态(200,201,500)
//	 * @throws IOException
//	 */
//	public static void showResultByInsert(HttpServletResponse response, String status) throws IOException{
//		final ObjectMapper objectMapper = new ObjectMapper();
//		response.setHeader("Content-Type",
//				"application/json; charset=UTF-8");
//		final JsonGenerator jsonGenerator = objectMapper.getFactory()
//				.createGenerator(response.getOutputStream(),
//						JsonEncoding.UTF8);
//		jsonGenerator.writeStartObject();
//		if(SystemConstant.SUCCESS.equals(status)){
//			jsonGenerator.writeStringField("code", "200");
//			jsonGenerator.writeStringField("msg", "接收成功");
//		}else if(SystemConstant.ERROR.equals(status)){
//			jsonGenerator.writeStringField("code", "201");
//			jsonGenerator.writeStringField("msg", "接收失败");
//		}else if(SystemConstant.SYSERROR.equals(status)){
//			jsonGenerator.writeStringField("code", "500");
//			jsonGenerator.writeStringField("msg", "服务器内部错误");
//		}else if(SystemConstant.EXISTING.equals(status)){
//			jsonGenerator.writeStringField("code", "202");
//			jsonGenerator.writeStringField("msg", "不允许重复上传");
//		}else if(SystemConstant.VER_ERROR.equals(status)){
//			jsonGenerator.writeStringField("code", "300");
//			jsonGenerator.writeStringField("msg", "验证码错误");
//		}
//		jsonGenerator.writeEndObject();
//		jsonGenerator.close();
//	}
	
//	/**
//	 * 查询成功后返回信息
//	 * @param status 状态(200,201,500)
//	 * @param result 结果集
//	 * @throws IOException
//	 */
//	public static void showResultBySearch(HttpServletResponse response, String status, String result, String method) throws IOException{
//		final ObjectMapper objectMapper = new ObjectMapper();
//		response.setHeader("Content-Type",
//				"application/json; charset=UTF-8");
//		final JsonGenerator jsonGenerator = objectMapper.getFactory()
//				.createGenerator(response.getOutputStream(),
//						JsonEncoding.UTF8);
//		jsonGenerator.writeStartObject();
//		if(SystemConstant.SUCCESS.equals(status)){
//			jsonGenerator.writeStringField("code", "200");
//			jsonGenerator.writeStringField("msg", "成功返回");
//			if("version".equals(method)){
//				Map<String, String> map = new LinkedHashMap<String, String>();
//				map.put("hexLink", result.split(",")[0]);
//				map.put("hexVersion", result.split(",")[1]);
//				map.put("apkLink", result.split(",")[2]);
//				map.put("apkVersion", result.split(",")[3]);
//				jsonGenerator.writeObjectField("data", map);
//			}else if("db".equals(method)){
//				Map<String, String> map = new LinkedHashMap<String, String>();
//				map.put("k", result.split(",")[0]);
//				map.put("b", result.split(",")[1]);
//				jsonGenerator.writeObjectField("data", map);
//			}
//		}else if(SystemConstant.ERROR.equals(status)){
//			jsonGenerator.writeStringField("code", "201");
//			if("version".equals(method)){
//				jsonGenerator.writeStringField("msg", "不存在");
//			}else if("db".equals(method)){
//				jsonGenerator.writeStringField("msg", "不存在K，B");
//			}
//
//		}else if(SystemConstant.SYSERROR.equals(status)){
//			jsonGenerator.writeStringField("code", "500");
//			jsonGenerator.writeStringField("msg", "服务器内部错误");
//		}
//		jsonGenerator.writeEndObject();
//		jsonGenerator.close();
//	}
	
	public static void show(HttpServletRequest request) {
		System.out.println("getRequestURL: " + request.getRequestURL());
		System.out.println("getRequestURI: " + request.getRequestURI());
		System.out.println("getQueryString: " + request.getQueryString());
		System.out.println("getRemoteAddr: " + request.getRemoteAddr());
		System.out.println("getRemoteHost: " + request.getRemoteHost());
		System.out.println("getRemoteUser: " + request.getRemoteUser());
		System.out.println("getMethod: " + request.getMethod());
		System.out.println("contenttype"+request.getContentType());
		System.out.println("length"+request.getContentLength());
		System.out.println("-------request.getParamterMap()-------");
		
		// 得到请求的参数Map，注意map的value是String数组类型
		Map map = request.getParameterMap();
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			String[] values = (String[]) map.get(key);
			for (String value : values) {
				System.out.println(key + "=" + value);
			}
		}
		System.out.println("--------request.getHeader()--------");
		// 得到请求头的name集合
		Enumeration<String> em = request.getHeaderNames();
		while (em.hasMoreElements()) {
			String name = (String) em.nextElement();
			String value = request.getHeader(name);
			System.out.println(name + "=" + value);
		}
	}

//	public static boolean auth(HttpServletResponse response, TestdataEntity testEntity) throws Exception{
//		final ObjectMapper objectMapper = new ObjectMapper();
//		response.setHeader("Content-Type",
//				"application/json; charset=UTF-8");
//		final JsonGenerator jsonGenerator = objectMapper.getFactory()
//				.createGenerator(response.getOutputStream(),
//						JsonEncoding.UTF8);
//		jsonGenerator.writeStartObject();
//		if(testEntity.getSign() == null || testEntity.getSign().isEmpty()) {
//			jsonGenerator.writeStringField("msg", "sign 错误");
//			jsonGenerator.writeEndObject();
//			jsonGenerator.close();
//			return false;
//		}else{
//			String machineId = testEntity.getMechineId();
//			String consnt = "acr";
//			String _sign = EncryptUtil.encryption(machineId + consnt);
//
//			if(!testEntity.getSign().equalsIgnoreCase(_sign)){
//				jsonGenerator.writeStringField("msg", "sign 错误");
//				jsonGenerator.writeEndObject();
//				jsonGenerator.close();
//				return  false;
//			}
//
//			if(StringUtils.ifNull(testEntity.getPatientIdCard()) ||
//					StringUtils.ifNull(testEntity.getTestResult()) ||
//					StringUtils.ifNull(testEntity.getMechineId()) ||
//					StringUtils.ifNull(testEntity.getTestProject()) ||
//					StringUtils.ifNull(testEntity.getResultTime())){
//
//				jsonGenerator.writeStringField("msg", "参数 错误");
//				jsonGenerator.writeEndObject();
//				jsonGenerator.close();
//
//				return false;
//			}
//
//			jsonGenerator.writeEndObject();
//			jsonGenerator.close();
//			return true;
//		}
//	}

	public static void main(String[] args) {
		String _sign = EncryptUtil.encryption("11" + "acr");
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		System.out.println(_sign);
	}
}
